<!--
 * @Author: your name
 * @Date: 2021-06-23 12:01:22
 * @LastEditTime: 2021-07-01 09:49:09
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \01e:\电商管理\app\src\components\Login.vue
-->
<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区 -->
      <el-form :model="loginFrom" class="login_from" :rules="loginFromRules" ref="loginFromRef">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginFrom.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginFrom.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login" :loading="loading_login">登录</el-button>
          <el-button type="info" @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginFrom: {
        //登录表单数据绑定对象
        username: "",
        password: "",
      },
      loginFromRules: {
        //验证规则
        username: [
          { required: true, message: "请输入用户名称", trigger: "change" },
          {
            min: 3,
            max: 11,
            message: "长度在 3到 11 个字符",
            trigger: "blur",
          },
        ], //验证用户名是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "change" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ], //验证密码
      },
      loading_login: false, //登录的loading状态
    };
  },
  methods: {
    resetLoginFrom() {
      // 点击重置按钮函数
      console.log(111);
      this.$refs.loginFromRef.resetFields();
      //  重置
    },
    // 登录
    login() {
      this.loading_login = true;
      this.$refs.loginFromRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginFrom);
        if (res.meta.status !== 200)
          return (
            this.$message.error("登陆失败，请重试！"),
            (this.loading_login = false)
          );
        this.$message.success("登陆成功");
        this.loading_login = false;
        // 1.将登陆成功之后的token，保存在客户端的sessionStroage中
        window.sessionStorage.setItem("token", res.data.token);
        // 1.1项目中出了登陆之外的其他api接口，必须是在登录之后才能访问
        // 1.2token只应该在当前的网站打开期间生效，所以将token保存在sessionStroage中

        // 2.通过编程导航方式跳转到后台主页，路由地址是/home
        this.$router.push("/home");
      });
    },
  },
  components: {},
};
</script>

<style scoped lang='scss'>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 20px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 100px;
      height: 100px;
      border: 2px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: relative;
      top: -60px;
      left: 50%;
      margin-left: -50px;
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
}
.login_from {
  position: absolute;
  width: 100%;
  bottom: 0;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: center;
}
</style>
